﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Diagnostics;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Net;

namespace FCNS.KeepMoneyLibrary.Data
{
    class ExchangeField
    {
       static string day = string.Empty;
        public static string UpdateDay
        {
            get { return day; }
        }

        static List<ExchangeField> fields = new List<ExchangeField>();
        public static List<ExchangeField> GetExchangeField
        {
            get
            {
                if (fields.Count == 0)
                    DownloadExchange(fields);

                return fields;
            }
        }

        public static void Init()
        {
            DownloadExchange(fields);
        }

        public ExchangeField(string name, string flag, string rate)
        {
            this.name = name;
            this.flag = flag;
            this.rate = rate;
        }

        string name = string.Empty;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        string flag = string.Empty;
        public string Flag
        {
            get { return flag; }
            set { flag = value; }
        }

        string rate = "1";
        public string Rate
        {
            get { return rate; }
            set { rate = value; }
        }


        private static void DownloadExchange(List<ExchangeField> es)
        {
            WebClient web = new WebClient();
            string str = System.Text.Encoding.Default.GetString(
                web.DownloadData("http://data.bank.hexun.com/ll/rmbhl.aspx"));

            web.Dispose();
            if (str.Length < 100)
                return ;

            //<div class="dzMain">
            //</table>
            try
            {
                int start = str.IndexOf("<div class=\"dzMain\">");
                str = str.Remove(0, start);

                start = str.IndexOf("<tr");
                int end = str.IndexOf("</tr") + 6;
                string one = str.Substring(start, end - start);
                str = str.Remove(0, end);

                start = str.IndexOf("<tr");
                end = str.IndexOf("</tr");
                string two = str.Substring(start, end - start);

                es.Clear();
                string[] strOne = one.Split(new string[] { "</th>" }, StringSplitOptions.RemoveEmptyEntries);
                string[] strTwo = two.Split(new string[] { "</td>" }, StringSplitOptions.RemoveEmptyEntries);

                for (int i = 1; i < strTwo.Length; i++)
                {
                    es.Add(new ExchangeField(strOne[i].Remove(0, strOne[i].LastIndexOf(">") + 1), "",
                        strTwo[i].Remove(0, strTwo[i].LastIndexOf(">") + 1)));
                }
                day= strTwo[0].Remove(0, strTwo[0].LastIndexOf(">") + 1);
            }
            catch {  }
        }

    }
}
